package org.jeecg.common.node;

import cn.hutool.core.util.StrUtil;

import java.util.List;

/**
 * @author by Administrator
 * @desc:
 * @since 2024-07-02 星期二 13:29:53
 */
public class ForestNodeMerger {

    public ForestNodeMerger() {
    }

    public static <T extends INode> List<T> merge(List<T> items) {
        ForestNodeManager<T> forestNodeManager = new ForestNodeManager(items);
        items.forEach((forestNode) -> {
            if (StrUtil.isNotBlank(forestNode.getParentId())) {
                INode node = forestNodeManager.getTreeNodeAT(forestNode.getParentId());
                if (node != null) {
                    node.getChildren().add(forestNode);
                } else {
                    forestNodeManager.addParentId(forestNode.getId());
                }
            }

        });
        return forestNodeManager.getRoot();
    }

}
